﻿using System;
using System.Data;
using System.Collections.Generic;
using Maticsoft.Common;
using CodeNotes.Model;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;
namespace CodeNotes.BLL
{
    /// <summary>
    /// CW_BookChapter
    /// </summary>
    public partial class CW_BookChapterLogic
    {
        private readonly CodeNotes.DAL.CW_BookChapter dal = new CodeNotes.DAL.CW_BookChapter();
        public CW_BookChapterLogic()
        { }
        #region  BasicMethod
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int Id)
        {
            return dal.Exists(Id);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(CodeNotes.Model.CW_BookChapter model)
        {
            return dal.Add(model);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(CodeNotes.Model.CW_BookChapter model)
        {
            return dal.Update(model);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int Id)
        {

            return dal.Delete(Id);
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool DeleteList(string Idlist)
        {
            return dal.DeleteList(Idlist);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public CodeNotes.Model.CW_BookChapter GetModel(int Id)
        {

            return dal.GetModel(Id);
        }

        /// <summary>
        /// 得到一个对象实体，从缓存中
        /// </summary>
        public CodeNotes.Model.CW_BookChapter GetModelByCache(int Id)
        {

            string CacheKey = "CW_BookChapterModel-" + Id;
            object objModel = Maticsoft.Common.DataCache.GetCache(CacheKey);
            if (objModel == null)
            {
                try
                {
                    objModel = dal.GetModel(Id);
                    if (objModel != null)
                    {
                        int ModelCache = Maticsoft.Common.ConfigHelper.GetConfigInt("ModelCache");
                        Maticsoft.Common.DataCache.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(ModelCache), TimeSpan.Zero);
                    }
                }
                catch { }
            }
            return (CodeNotes.Model.CW_BookChapter)objModel;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            return dal.GetList(strWhere);
        }
        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            return dal.GetList(Top, strWhere, filedOrder);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<CodeNotes.Model.CW_BookChapter> GetModelList(string strWhere)
        {
            DataSet ds = dal.GetList(strWhere);
            return DataTableToList(ds.Tables[0]);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<CodeNotes.Model.CW_BookChapter> DataTableToList(DataTable dt)
        {
            List<CodeNotes.Model.CW_BookChapter> modelList = new List<CodeNotes.Model.CW_BookChapter>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                CodeNotes.Model.CW_BookChapter model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = dal.DataRowToModel(dt.Rows[n]);
                    if (model != null)
                    {
                        modelList.Add(model);
                    }
                }
            }
            return modelList;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetAllList()
        {
            return GetList("");
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            return dal.GetRecordCount(strWhere);
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            return dal.GetListByPage(strWhere, orderby, startIndex, endIndex);
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        //public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        //{
        //return dal.GetList(PageSize,PageIndex,strWhere);
        //}

        #endregion  BasicMethod
        #region  ExtensionMethod
        /// <summary>
        /// 获得数据列表，固定字段，文章内容搜索出来太慢
        /// </summary>
        public List<CodeNotes.Model.CW_BookChapter> GetFixedColumnModelList(string strFields,string strWhere)
        {
            DataTable dt = dal.GetFixedColumnList(strFields,strWhere).Tables[0];

            List<CodeNotes.Model.CW_BookChapter> modelList = new List<CodeNotes.Model.CW_BookChapter>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                CodeNotes.Model.CW_BookChapter model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = FixedDataRowToModel(dt.Rows[n]);
                    if (model != null)
                    {
                        modelList.Add(model);
                    }
                }
            }

            return modelList;
        }

        public CodeNotes.Model.CW_BookChapter FixedDataRowToModel(DataRow row)
        {
            CodeNotes.Model.CW_BookChapter model = new CodeNotes.Model.CW_BookChapter();
            if (row != null)
            {
                var col = row.Table.Columns;
                
                if (col.Contains("Id")&&row["Id"] != null && row["Id"].ToString() != "")
                {
                    model.Id = int.Parse(row["Id"].ToString());
                }
                if (col.Contains("BookId") && row["BookId"] != null && row["BookId"].ToString() != "")
                {
                    model.BookId = int.Parse(row["BookId"].ToString());
                }
                if (col.Contains("ChapterName") && row["ChapterName"] != null)
                {
                    model.ChapterName = row["ChapterName"].ToString();
                }
                if (col.Contains("ChapterContent") && row["ChapterContent"] != null)
                {
                    model.ChapterContent = row["ChapterContent"].ToString();
                }
                if (col.Contains("ClickCount") && row["ClickCount"] != null && row["ClickCount"].ToString() != "")
                {
                    model.ClickCount = int.Parse(row["ClickCount"].ToString());
                }
                if (col.Contains("Source") && row["Source"] != null)
                {
                    model.Source = row["Source"].ToString();
                }
                if (col.Contains("CreateDate") && row["CreateDate"] != null && row["CreateDate"].ToString() != "")
                {
                    model.CreateDate = DateTime.Parse(row["CreateDate"].ToString());
                }
                if (col.Contains("Sort") && row["Sort"] != null && row["Sort"].ToString() != "")
                {
                    model.Sort = int.Parse(row["Sort"].ToString());
                }
            }
            return model;
        }

        public void addClickCount(int chapterid)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update CW_BookChapter set ClickCount=ClickCount+1 ");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
            parameters[0].Value = chapterid;
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }
        #endregion  ExtensionMethod
    }
}

